<?php 

class openft{
	
	public $db;
	public $res=array();
	//未中奖名单
	public $array;
	public $noluckUser;
	public function __construct($cof,$data,$array)
	{
		$this->db=new dbli($cof);
		$this->array=$array;
		
		$this->res[1]=array(
		    $data['qh'],
		    $data['jg'],
		    $data['time'],
		);
	}
	
	public function oneOpen()
	{
		$this->CheckOpen();
	}
	
	public function CheckOpen(){
		//查结果
		
	
		$this->openAll();
		
	}
	
	public function openAll()
	{
		/* 	插入数据 */
		$this->insetdate();
		/* 获取当期未开奖的下注信息 */
		$arr=$this->checkDate();
		
		if($arr){
			foreach ($arr as $key)
			{
				$this->see($key);
			}
			$this->noluck();
		}else{
			echo date('Y-m-d H:i:s',time()). ' 暂无开奖数据'.PHP_EOL;
		}
		
	}
	
	/* 开奖 */
	public function see($key)
	{
		
		$n=explode(',',$this->res[1][1]);
		
		switch($key['stype'])
		{
			case 'ABC':
				 $this->ABCdo($key,$n);
				break;
			case  '和':
				$this->hezhi($key,$n);
				break;
			case '组':
				$this->zu($key,$n);
				break;
			case '庄闲':
				$this->zhuangxian($key,$n);
				break;
			case '龙':
				$this->long($key,$n);
				break;
			case  '虎':
				$this->hu($key,$n);
				break;
			case  '大':
				$this->dxds($key,$n,'大');
				break;
			case  '小':
				$this->dxds($key,$n,'小');
				break;
			case  '单':
				$this->dxds($key,$n,'单');
				break;
			case  '双':
				$this->dxds($key,$n,'双');
				break;
			case '大单':
				$this->Mdxds($key,$n,'大单');
				break;
			case '大双':
				$this->Mdxds($key,$n,'大双');
				break;
			case '小单':
				$this->Mdxds($key,$n,'小单');
				break;
			case '小双':
				$this->Mdxds($key,$n,'小双');
				break;
			case 0:
				$this->Mnumbers($key,$n);
				break;
			case 1:
				$this->Mnumbers($key,$n);
				break;
			case 2:
				$this->Mnumbers($key,$n);
				break;
			case 3:
				$this->Mnumbers($key,$n);
				break;
			case 4:
				$this->Mnumbers($key,$n);
				break;
			case 5:
				$this->Mnumbers($key,$n);
				break;
			case 6:
				$this->Mnumbers($key,$n);
				break;
			case 7:
				$this->Mnumbers($key,$n);
				break;
			case 8:
				$this->Mnumbers($key,$n);
				break;
			case 9:
				$this->Mnumbers($key,$n);
				break;
		}
		
	}
	
	public function Mnumbers($key,$n)
	{
		$array=$this->array;
		if($key['stype']==0){
			$key['stype']=10;
		}
		$chedao=$key['stype']-1;
		if($n[$chedao]==$key['nums']){
			$this->lucky($key,$array[$key['stype']]);
		}else{
			$this->noluckUser[]=$key['ID'];
		}
	}
	
	
	public function Mdxds($key,$n,$v)
	{
		$array=$this->array;
		$type=$key['nums']-1;
		
		//file_put_contents('C:/s.txt',$n[$type].PHP_EOL,FILE_APPEND);
		switch ($v)
		{
			case '大单':
				if($n[$type]>5 && $n[$type]%2==1)
				{
					$this->lucky($key,$array['大单']);
				}else{
					$this->noluckUser[]=$key['ID'];
				}
				break;
			case '小单':
				
				if($n[$type]<=5 && $n[$type]%2==1)
				{
					$this->lucky($key,$array['小单']);
				}else{
					$this->noluckUser[]=$key['ID'];
				}
				break;
			case '小双':
				
				if($n[$type]<=5 && $n[$type]%2==0)
				{
					$this->lucky($key,$array['小双']);
				}else{
					$this->noluckUser[]=$key['ID'];
				}
				
				break;
			case '大双':
				if($n[$type]>5 && $n[$type]%2==0)
				{
					$this->lucky($key,$array['大双']);
				}else{
					$this->noluckUser[]=$key['ID'];
				}
				break;
		}
	}
	
	public function dxds($key,$n,$v)
	{
		$array=$this->array;
		$type=$key['nums']-1;
		switch ($v)
		{
			case '大':
					$n[$type]>5?$this->lucky($key,$array['大']):$this->noluckUser[]=$key['ID'];
					break;
			case '小':
					$n[$type]<=5?$this->lucky($key,$array['小']):$this->noluckUser[]=$key['ID'];
					break;
			case '单':
					$n[$type]%2==1?$this->lucky($key,$array['单']):$this->noluckUser[]=$key['ID'];
					break;
			case '双':
				
					$n[$type]%2==0?$this->lucky($key,$array['双']):$this->noluckUser[]=$key['ID'];
					break;
		}
	}
	
	
	public function hu($key,$n)
	{
		$array=$this->array;
	
		switch ($key['nums']){
			case 1:
				$n[0]<$n[9]?$this->lucky($key,$array['虎']):$this->noluckUser[]=$key['ID'];
				break;
			case 2:
				$n[1]<$n[8]?$this->lucky($key,$array['虎']):$this->noluckUser[]=$key['ID'];
				break;
			case 3:
				$n[2]<$n[7]?$this->lucky($key,$array['虎']):$this->noluckUser[]=$key['ID'];
				break;
			case 4:
				$n[3]<$n[6]?$this->lucky($key,$array['虎']):$this->noluckUser[]=$key['ID'];
				break;
			case 5:
				$n[4]<$n[5]?$this->lucky($key,$array['虎']):$this->noluckUser[]=$key['ID'];
				break;
		}
	}
	
	
	public function long($key,$n)
	{
		$array=$this->array;
		switch ($key['nums']){
			case 1:
				    $n[0]>$n[9]?$this->lucky($key,$array['龙']):$this->noluckUser[]=$key['ID'];
				    break;
			case 2:
					$n[1]>$n[8]?$this->lucky($key,$array['龙']):$this->noluckUser[]=$key['ID'];
					break;
			case 3:
					$n[2]>$n[7]?$this->lucky($key,$array['龙']):$this->noluckUser[]=$key['ID'];
					break;
			case 4:
					$n[3]>$n[6]?$this->lucky($key,$array['龙']):$this->noluckUser[]=$key['ID'];
					break;
			case 5:
					$n[4]>$n[5]?$this->lucky($key,$array['龙']):$this->noluckUser[]=$key['ID'];
					break;
		}
	}
	
	
	public function zhuangxian($key,$n){
		$array=$this->array;
		if($key['nums']=='庄' && $n[0]>$n[1])
		{
			$this->lucky($key,$array['庄']);
		}elseif($key['nums']=='闲' && $n[0]<$n[1]){
			$this->lucky($key,$array['闲']);
		}else{
			$this->noluckUser[]=$key['ID'];
		}
	}
	public function zu($key,$n)
	{
		$array=$this->array;
		$zu=explode('-',$key['nums']);
		$zuarr=array($n[0],$n[1]);
		if(in_array($zu[0],$zuarr) && in_array($zu[1],$zuarr))
		{
			$this->lucky($key,$array['组']);
		}else{
			$this->noluckUser[]=$key['ID'];
		}
	}
	/* 和  --大小单双 或者数字*/
	public function hezhi($key,$n)
	{
		$array=$this->array;
		if(is_numeric($key['nums'])){
			$hezhi=$n[0]+$n[1];
			if($hezhi==$key['nums']){
				$this->lucky($key,$array['和'.$hezhi]);
			}else{
				$this->noluckUser[]=$key['ID'];
			}
		}elseif($key['nums']=='大'){
			$hezhi=$n[0]+$n[1];
			if($hezhi>11&& $hezhi<=19){
				$this->lucky($key,$array['和大']);
			}else{
				$this->noluckUser[]=$key['ID'];
			}
		}elseif($key['nums']=='小'){
			$hezhi=$n[0]+$n[1];
			if($hezhi>=3&& $hezhi<=11){
				$this->lucky($key,$array['和小']);
			}else{
				$this->noluckUser[]=$key['ID'];
			}
		}elseif($key['nums']=='单'){
			$hezhi=$n[0]+$n[1];
			if($hezhi%2==1){
				$this->lucky($key,$array['和单']);
			}else{
				$this->noluckUser[]=$key['ID'];
			}
		}elseif($key['nums']=='双'){
			$hezhi=$n[0]+$n[1];
			if($hezhi%2==0){
				$this->lucky($key,$array['和双']);
			}else{
				$this->noluckUser[]=$key['ID'];
			}
		}
	}
	
	/*ABC  */
	public function ABCdo($key,$n){
		$array=$this->array;
		if($key['nums']=='A'){
			$hezhi=$n[0]+$n[1];
			if($hezhi>3&& $hezhi<=7){
				$this->lucky($key,$array['A']);
			}else{
				$this->noluckUser[]=$key['ID'];
			}
		}elseif($key['nums']=='B'){
			$hezhi=$n[0]+$n[1];
			if($hezhi>7&& $hezhi<=14){
				$this->lucky($key,$array['B']);
			}else{
				$this->noluckUser[]=$key['ID'];
			}
		}elseif($key['nums']=='C'){
			$hezhi=$n[0]+$n[1];
			if($hezhi>14&& $hezhi<=19){
				$this->lucky($key,$array['C']);
			}else{
				$this->noluckUser[]=$key['ID'];
			}
		}
	}
	
	/* 中奖处理 */
	
	
	public function lucky($key,$bl)
	{
		$money=$bl*$key['money'];
		$time=time();
		$usermessage=$this->db->select("SELECT * FROM `wx_manage` WHERE ID={$key['uid']}");
		$nmoney=$money+$usermessage[0]['money'];
		$this->db->U_D('START TRANSACTION');
		$sql1="UPDATE `wx_manage` SET `money`=$nmoney WHERE ID={$key['uid']}";
		$sql2="UPDATE `wx_pk10` SET `is_money`=2,`oktime`=$time,`zmoney`=$money WHERE ID={$key['ID']}";

		if($key['stype']=='虎' || $key['stype']=='龙' || $key['stype']=='大' || $key['stype']=='小' || $key['stype']=='单' || $key['stype']=='双' || $key['stype']=='大双' || $key['stype']=='小双' || $key['stype']=='大单' || $key['stype']=='小单'){
                	$strMFD="『{$key['nums']}/{$key['stype']}/{$key['money']}』";
            	}else{
			$strMFD="『{$key['stype']}/{$key['nums']}/{$key['money']}』";
            	   
           	 }

		$str="竞猜中奖，期号：{$key['issue']} ,中奖点数：$money,$strMFD";
		$sql3="INSERT INTO `user_log` ( `uid`, `money`, `old_money`, `new_money`, `type`, `status`, `remark`,`issue`,`gtime`,`yx`) VALUES ({$key['uid']},$money,{$usermessage[0]['money']},$nmoney,1,2,'$str','{$key['issue']}',$time,2)";
		
		 if($this->db->U_D($sql1) && $this->db->U_D($sql2) &&  $this->db->add($sql3))
		{
			$this->db->U_D('COMMIT');
			
		}else{
			file_put_contents('shibaisql.txt',$sql1.PHP_EOL.$sql2.PHP_EOL.$sql3.PHP_EOL,FILE_APPEND);
			$this->db->U_D('ROLLBACK');
		}
	}
	
	/*  未中奖处理 */
	public function noluck()
	{
		$arr=$this->noluckUser;
		$time=time();
		$sql="UPDATE `wx_pk10` SET `is_money`=3 ,`oktime`=$time WHERE ID in(";
		foreach ($arr as  $key)
		{
			$sql.=$key.',';
		}
		
		$sql=substr($sql,0,strlen($sql)-1).')';
		//echo $sql.PHP_EOL;
		 if(!$this->db->U_D($sql))
		{
			
			file_put_contents('./noLUCKY_err.log',$sql.PHP_EOL,FILE_APPEND);
		}
	}

	/* 获取当期未开奖的下注信息 */
	public function checkDate()
	{
		$sql="select * from `wx_pk10` where issue='{$this->res[1][0]}' and is_money=1 and oktime=0 and games=2";
		//echo $sql.PHP_EOL;
		return $this->db->select($sql);
	}
	
	
	/* 16  大 双 闲 C  */
	public function insetdate()
	{
		$res=$this->res;
		$opennum=explode(',',$res[1][1]);
		$gtime=strtotime($res[1][2]);
		$dtime=time();
		$TMLH=$this->setTM($opennum);
		$sql="INSERT INTO `pks_k`( `issue`, `numbers`, `tm`, `lh`, `gtime`, `dtime`,`games`) VALUES ('{$res[1][0]}','{$res[1][1]}','{$TMLH[0]}','{$TMLH[1]}',$gtime,$dtime,'2')";
		
		if($this->db->add($sql))
		{
			echo date('Y-m-d H:i:s',time()).iconv('UTF-8', 'GB2312', ' 飞艇开始开奖！！ ') .PHP_EOL;
		}

	}
	
	public function setTM($n){
		$tm='';
		$lh='';
		$hezhi=$n[0]+$n[1];
		$daxiao=$n[0]>=6?'大':'小';
		$danshuang=$n[0]%2==0?'双':'单';
		$zhuangxian=$n[0]>$n[1]?'庄':'闲';
		if($hezhi>3 && $hezhi<=7){
			$boduan='A';
		}elseif($hezhi>7 && $hezhi<=14){
			$boduan='B';
		}elseif($hezhi>14 && $hezhi<=19){
			$boduan='C';
		}
		
		$tm="$hezhi,$daxiao,$danshuang,$zhuangxian,$boduan";
		
		if($n[0]>$n[9]){
			$lh.="龙";
		}else{
			$lh.="虎";
		}
		
		if($n[1]>$n[8]){
			$lh.="龙";
		}else{
			$lh.="虎";
		}
		
		if($n[2]>$n[7]){
			$lh.="龙";
		}else{
			$lh.="虎";
		}
		
		if($n[3]>$n[6]){
			$lh.="龙";
		}else{
			$lh.="虎";
		}
		
		if($n[4]>$n[5]){
			$lh.="龙";
		}else{
			$lh.="虎";
		}
		
		return array($tm,$lh);
	}
	
	
	
}



?>